package org.ktx.osr.data

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import org.ktx.osr.ui.AppSettings

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "app_settings")

class SettingsRepository(private val context: Context) {
    
    // 定义设置键
    private object PreferencesKeys {
        val RESOLUTION = stringPreferencesKey("resolution")
        val FRAME_RATE = stringPreferencesKey("frame_rate")
        val VIDEO_FORMAT = stringPreferencesKey("video_format")
        val INCLUDE_SYSTEM_SOUND = booleanPreferencesKey("include_system_sound")
        val INCLUDE_MICROPHONE = booleanPreferencesKey("include_microphone")
        val INCLUDE_CAMERA = booleanPreferencesKey("include_camera")
        val SAVE_LOCATION = stringPreferencesKey("save_location")
    }
    
    // 获取所有设置的Flow
    val settings: Flow<AppSettings> = context.dataStore.data.map { preferences ->
        AppSettings(
            resolution = preferences[PreferencesKeys.RESOLUTION] ?: "1080p",
            frameRate = preferences[PreferencesKeys.FRAME_RATE] ?: "30 fps",
            videoFormat = preferences[PreferencesKeys.VIDEO_FORMAT] ?: "MP4",
            includeSystemSound = preferences[PreferencesKeys.INCLUDE_SYSTEM_SOUND] ?: true,
            includeMicrophone = preferences[PreferencesKeys.INCLUDE_MICROPHONE] ?: false,
            includeCamera = preferences[PreferencesKeys.INCLUDE_CAMERA] ?: false,
            saveLocation = preferences[PreferencesKeys.SAVE_LOCATION] ?: "Internal Storage"
        )
    }
    
    // 更新分辨率
    suspend fun updateResolution(resolution: String) {
        context.dataStore.edit { preferences ->
            preferences[PreferencesKeys.RESOLUTION] = resolution
        }
    }
    
    // 更新帧率
    suspend fun updateFrameRate(frameRate: String) {
        context.dataStore.edit { preferences ->
            preferences[PreferencesKeys.FRAME_RATE] = frameRate
        }
    }
    
    // 更新视频格式
    suspend fun updateVideoFormat(videoFormat: String) {
        context.dataStore.edit { preferences ->
            preferences[PreferencesKeys.VIDEO_FORMAT] = videoFormat
        }
    }
    
    // 更新系统声音设置
    suspend fun updateIncludeSystemSound(include: Boolean) {
        context.dataStore.edit { preferences ->
            preferences[PreferencesKeys.INCLUDE_SYSTEM_SOUND] = include
        }
    }
    
    // 更新麦克风设置
    suspend fun updateIncludeMicrophone(include: Boolean) {
        context.dataStore.edit { preferences ->
            preferences[PreferencesKeys.INCLUDE_MICROPHONE] = include
        }
    }
    
    // 更新摄像头设置
    suspend fun updateIncludeCamera(include: Boolean) {
        context.dataStore.edit { preferences ->
            preferences[PreferencesKeys.INCLUDE_CAMERA] = include
        }
    }
    
    // 更新保存位置
    suspend fun updateSaveLocation(location: String) {
        context.dataStore.edit { preferences ->
            preferences[PreferencesKeys.SAVE_LOCATION] = location
        }
    }
    
    // 批量更新设置
    suspend fun updateSettings(settings: AppSettings) {
        context.dataStore.edit { preferences ->
            preferences[PreferencesKeys.RESOLUTION] = settings.resolution
            preferences[PreferencesKeys.FRAME_RATE] = settings.frameRate
            preferences[PreferencesKeys.VIDEO_FORMAT] = settings.videoFormat
            preferences[PreferencesKeys.INCLUDE_SYSTEM_SOUND] = settings.includeSystemSound
            preferences[PreferencesKeys.INCLUDE_MICROPHONE] = settings.includeMicrophone
            preferences[PreferencesKeys.INCLUDE_CAMERA] = settings.includeCamera
            preferences[PreferencesKeys.SAVE_LOCATION] = settings.saveLocation
        }
    }
    
    // 获取当前设置（同步方法，用于Service中）
    suspend fun getCurrentSettings(): AppSettings {
        return settings.first()
    }
}
